package xc.com.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import xc.com.common.Result;
import xc.com.common.StatusCode;
import xc.com.course.domain.Teachplan;
import xc.com.course.domain.TeachplanMedia;
import xc.com.course.mapper.TeachplanMapper;
import xc.com.course.mapper.TeachplanMediaMapper;
import xc.com.course.service.TeachplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuolizhong
 * @since 2021-06-03
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private  TeachplanMapper teachplanMapper;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public Result addTeachplan(Teachplan teachplan) {
        int insert = teachplanMapper.insert(teachplan);
        if(insert>0){
            return new Result(true, StatusCode.OK,"添加成功");
        }
        return new Result(false, StatusCode.OK,"添加失败");
    }

    @Override
    public  Teachplan[] findByCourseId(String courseId) {
        QueryWrapper<Teachplan> wrapper=new QueryWrapper<>();
        wrapper.eq("courseid",courseId);
        wrapper.eq("parentid","0");
        //查询一分类
        List<Teachplan> list = teachplanMapper.selectList(wrapper);
        if(list==null){
            return null;
        }
        for (Teachplan teachplan : list) {
            QueryWrapper<Teachplan> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("parentid",teachplan.getId());
            //查询二级分类
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            for (Teachplan teachplan1 : teachplans) {
                QueryWrapper<Teachplan> teachplanQueryWrapper=new QueryWrapper<>();
                teachplanQueryWrapper.eq("parentid",teachplan1.getId());
                //查询三级分类
                List<Teachplan> teachplanList = teachplanMapper.selectList(teachplanQueryWrapper);
                teachplan1.setTeachplans(teachplanList);
            }
            teachplan.setTeachplans(teachplans);
        }
        Teachplan[] teachplans = new Teachplan[list.size()];
        for (int i=0;i<list.size();i++){
            teachplans[i]= list.get(i);
        }
        return teachplans;
    }

    @Override
    public Result deleteByIds(String[] ids) {
        if (ids.length>=1){
            for (int i = 0; i <ids.length; i++) {
                System.out.println(ids[i]);
            }
        }
        return null;
    }

    @Override
    public Result deleteByCourseId(String courseId) {
        return null;
    }

    @Override
    public Teachplan findAllTeachPlan(String courseId) {
        QueryWrapper<Teachplan> wrapper=new QueryWrapper<>();
        wrapper.eq("courseid",courseId);
        wrapper.eq("parentid","0");
        //查询一分类
        Teachplan teachplan = teachplanMapper.selectOne(wrapper);
        if(teachplan==null){
            return null;
        }
        QueryWrapper<Teachplan> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("courseid",courseId);
        queryWrapper.eq("parentid",teachplan.getId());

        List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
        if(teachplans==null){
            return teachplan;
        }
        for (Teachplan teach : teachplans) {
            QueryWrapper<TeachplanMedia> teachplanQueryWrapper=new QueryWrapper<>();
            teachplanQueryWrapper.eq("teachplan_id",teach.getId());
            TeachplanMedia media = teachplanMediaMapper.selectOne(teachplanQueryWrapper);
            teach.setTeachplanMedia(media);
            QueryWrapper<Teachplan> wrappers=new QueryWrapper<>();
            queryWrapper.eq("courseid",courseId);
            queryWrapper.eq("parentid",teach.getId());
            List<Teachplan> teachplanList = teachplanMapper.selectList(queryWrapper);
            for (Teachplan teachplan1 : teachplanList) {
                TeachplanMedia teachplanMedia = teachplanMediaMapper.selectById(teachplan1.getId());
                teachplan1.setTeachplanMedia(teachplanMedia);
            }
            teach.setTeachplans(teachplanList);
        }
        teachplan.setTeachplans(teachplans);
        return teachplan;
    }
}
